import sys
import os
sys.path.append(os.getcwd()) # 将当前目录加入sys.path

from ui.Ui_arimaWindow import Ui_MainWindow
from model.arima_model import Arima_model
from PyQt5 import QtCore, QtGui, QtWidgets
import cv2

class arimaWindow(QtWidgets.QMainWindow, Ui_MainWindow):    
    # 切换ui
    arima2main = QtCore.pyqtSignal()  
    openCalculate = QtCore.pyqtSignal()

    p = 4
    d = 1
    q = 3
    
    def __init__(self):
        # super().setupUi(Dialog)
        super(arimaWindow, self).__init__()
        self.setupUi(self)
        
        self.file = None #要处理的文件
        # 注释
        self.file = 'data\\sentiment.csv'
        self.Primarykey = 'UMCSENT'
        #data
        self.train_data = None
        self.test_data = None
        self.diff_data = None
        #buttons
        self.browseButton.clicked.connect(self.browsefile)
        self.openButton.clicked.connect(self.openfile)
        self.downloadButton.clicked.connect(self.download)
        self.uploadButton.clicked.connect(self.upload)
        self.reloadButton.clicked.connect(self.reload)
        #lineedit
        self.lineEdit.setPlaceholderText(self.file)
        self.lineEdit.setReadOnly(True)
        #pages
        self.page1Button.clicked.connect(self.page1)
        self.page2Button.clicked.connect(self.page2)
        self.page3Button.clicked.connect(self.page3)        
        self.page4Button.clicked.connect(self.page4)
        self.page5Button.clicked.connect(self.page5)        
        self.page6Button.clicked.connect(self.page6)
        #uiButtons
        self.applyButton.clicked.connect(self.apply)
        self.trainButton.clicked.connect(self.train)
        self.calculateButton.clicked.connect(self.calculate)        
        self.selectallButton.clicked.connect(self.selectAll)
        #画图
        self.arimaCheck.setChecked(True)
        self.spinBox_p.setValue(self.p)
        self.spinBox_d.setValue(self.d)
        self.spinBox_q.setValue(self.q)
        self.showGraphics()

    def showGraphics(self):
        arima_graph11 = Arima_model(width=3.6, heigh=1.6) 
        arima_graph12 = Arima_model(width=3.6, heigh=1.6)
        arima_graph21 = Arima_model(width=3.6, heigh=1.8)     
        arima_graph22 = Arima_model(width=3.6, heigh=1.8)
        arima_graph31 = Arima_model(width=3.6, heigh=3.8)
        arima_graph41 = Arima_model(width=3.6, heigh=3.8)
        arima_graph61 = Arima_model(width=3.6, heigh=1.6)
        arima_graph62 = Arima_model(width=3.6, heigh=1.6)
        arima_graph51 = Arima_model(width=3.6, heigh=3.8)

        self.train_data, self.test_data = arima_graph11.data_resampling(self.file, self.Primarykey, part=0.88) 
        graphicsscene_11 = QtWidgets.QGraphicsScene() 
        graphicsscene_11.addWidget(arima_graph11) 
        self.graphicsView_11.setScene(graphicsscene_11) 
        self.graphicsView_11.show()

        self.diff_data = arima_graph12.data_stationarity(self.train_data[[self.Primarykey]], d=1) 
        graphicsscene_12 = QtWidgets.QGraphicsScene() 
        graphicsscene_12.addWidget(arima_graph12) 
        self.graphicsView_12.setScene(graphicsscene_12) 
        self.graphicsView_12.show()

        arima_graph21.plt_acf(self.diff_data[[self.Primarykey]])        
        graphicsscene_21 = QtWidgets.QGraphicsScene() 
        graphicsscene_21.addWidget(arima_graph21) 
        self.graphicsView_21.setScene(graphicsscene_21) 
        self.graphicsView_21.show()

        arima_graph22.plt_pacf(self.diff_data[[self.Primarykey]]) 
        graphicsscene_22 = QtWidgets.QGraphicsScene() 
        graphicsscene_22.addWidget(arima_graph22) 
        self.graphicsView_22.setScene(graphicsscene_22) 
        self.graphicsView_22.show()
        
        # self.AIC = arima_graph31.determinante_AIC(self.diff_data[self.Primarykey])
        # print(self.AIC) # (5, 4)
        # arima_graph31.heatmap_AIC(self.diff_data[[self.Primarykey]]) 
        # graphicsscene_31 = QtWidgets.QGraphicsScene() 
        # graphicsscene_31.addWidget(arima_graph31) 
        # self.graphicsView_31.setScene(graphicsscene_31) 
        # self.graphicsView_31.show()
        
        # self.BIC = arima_graph41.determinante_BIC(self.diff_data[self.Primarykey])
        # print(self.BIC) # (4, 3)
        # arima_graph41.heatmap_BIC(self.diff_data[[self.Primarykey]]) 
        # graphicsscene_41 = QtWidgets.QGraphicsScene() 
        # graphicsscene_41.addWidget(arima_graph41) 
        # self.graphicsView_41.setScene(graphicsscene_41) 
        # self.graphicsView_41.show()   

        # self.p, self.q = self.AIC
        self.p, self.d, self.q = 3, 1, 2
        order_ = (self.p, self.d, self.q)
        
        arima_graph51.model_evaluate(self.train_data[[self.Primarykey]], order_) 
        graphicsscene_51 = QtWidgets.QGraphicsScene() 
        graphicsscene_51.addWidget(arima_graph51) 
        self.graphicsView_51.setScene(graphicsscene_51) 
        self.graphicsView_51.show() 

        arima_graph61.in_sample_prediction(self.train_data[[self.Primarykey]], order_)
        graphicsscene_61 = QtWidgets.QGraphicsScene() 
        graphicsscene_61.addWidget(arima_graph61) 
        self.graphicsView_61.setScene(graphicsscene_61) 
        self.graphicsView_61.show() 
        
        self.fc_data = arima_graph62.out_sample_prediction(self.train_data[[self.Primarykey]], self.test_data[[self.Primarykey]], order_)
        graphicsscene_62 = QtWidgets.QGraphicsScene() 
        graphicsscene_62.addWidget(arima_graph62) 
        self.graphicsView_62.setScene(graphicsscene_62) 
        self.graphicsView_62.show() 

    def page1(self):
        self.stackedWidget.setCurrentIndex(0)   
    def page2(self):
        self.stackedWidget.setCurrentIndex(1)    
    def page3(self):
        self.stackedWidget.setCurrentIndex(2)
    def page4(self):
        self.stackedWidget.setCurrentIndex(3)
    def page5(self):
        self.stackedWidget.setCurrentIndex(4)
    def page6(self):
        self.stackedWidget.setCurrentIndex(5)
        
    def openfile(self):
        msg_box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, 'Waring', 'File already exists!\n\nPress Reload to upload the file again.')
        msg_box.exec_()
    
    def browsefile(self):
        msg_box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, 'Waring', 'File already exists!\n\nPress Reload to upload the file again.')
        msg_box.exec_() 
    
    def upload(self):
        msg_box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, 'Waring', 'File already exists!\n\nPress Reload to upload the file again.')
        msg_box.exec_()
        
    def reload(self):
        self.arima2main.emit()

    def download(self):
        #保存图片
        pass

    def apply(self):
        self.p = self.spinBox_p.value()
        self.d = self.spinBox_d.value()
        self.q = self.spinBox_q.value()
        self.showGraphics()
    
    def train(self):
        pass
        # if self.informerCheck.isChecked():
        #     self.arima2informer.emit()
        # self.arima2main.emit()
    
    def calculate(self):
        if (self.mfeCheck.isChecked() | self.maeCheck.isChecked() | self.mapeCheck.isChecked() | self.mpeCheck.isChecked() | self.mseCheck.isChecked() | self.sseCheck.isChecked() | self.smseCheck.isChecked() | self.rmseCheck.isChecked() | self.nmseCheck.isChecked()) == False:
            msg_box = QtWidgets.QMessageBox(QtWidgets.QMessageBox.Warning, 'Waring', 'Please select a error at least.')
            msg_box.exec_()
        else:
            self.openCalculate.emit()

    def selectAll(self):
        self.mfeCheck.setChecked(True)
        self.maeCheck.setChecked(True)
        self.mapeCheck.setChecked(True)
        self.mpeCheck.setChecked(True)
        self.mseCheck.setChecked(True)
        self.sseCheck.setChecked(True)
        self.smseCheck.setChecked(True)
        self.rmseCheck.setChecked(True)
        self.nmseCheck.setChecked(True)